//
//  DataHandel.m
//  StationManager
//
//  Created by 泽华伟业 on 17/3/7.
//  Copyright © 2017年 Helong. All rights reserved.
//

#import "DataHandel.h"
#import "AFNwebManager.h"
#import "StationInfoModel.h"
#import "StatusModel.h"

static DataHandel *_dataHandel = nil;
@implementation DataHandel
//单例初始化
+ (DataHandel *)sharedDataHandel{
    return [[self alloc]init];
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone{
    
    @autoreleasepool {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            if (_dataHandel == nil) {
                _dataHandel = [super allocWithZone:zone];
            }
        });
        return _dataHandel;
    }
}

#pragma mark -- 获取全部站点信息
- (void)getAllStationInfoListWIthBlock:(GETDATABLOCK)allStationBlock{
    NSDictionary *dict = @{@"resultType" : @"json"};
    [[AFNwebManager shareAFNWebManager]SOAPData:dict soapMethod:@"GetStationInfoList" success:^(id responseObject) {
        NSArray *objectAry = (NSArray *)responseObject;
        NSMutableArray *stationArray = [NSMutableArray array];
        for (NSDictionary *dict in objectAry) {
            StationInfoModel *stationInfoModel = [[StationInfoModel alloc]init];
            stationInfoModel.position = [dict objectForKey:@"位置"];
            stationInfoModel.stationStatus = [dict objectForKey:@"台站状态"];
            stationInfoModel.navigationPosition = [dict objectForKey:@"导航位置"];
            stationInfoModel.stationNum = [dict objectForKey:@"站号"];
            stationInfoModel.stationName = [dict objectForKey:@"站名"];
            stationInfoModel.stationType = [dict objectForKey:@"站点类型"];
            stationInfoModel.latitude = [dict objectForKey:@"纬度"];
            stationInfoModel.longitude = [dict objectForKey:@"经度"];
            [stationArray addObject:stationInfoModel];
        }
        if (stationArray.count != 0) {
            allStationBlock(stationArray);
        }
        
    } failure:^{
        
    }];
}

#pragma mark -- 获取温度信息
- (void)getTemperatureDataWithBlock:(GETDATABLOCK)temperatureData{
    NSDictionary *dict = @{@"resultType" : @"json"};
    [[AFNwebManager shareAFNWebManager]SOAPData:dict soapMethod:@"GetTabTemperatureData" success:^(id responseObject) {
        temperatureData(responseObject);
    } failure:^{
        
    }];
}

#pragma mark -- 获取降水数据
- (void)getRainDataWithHour:(int)hour Block:(GETDATABLOCK)rainDataBlock{
    NSDictionary *rainDict = @{
                               @"resultType" : @"json",
                               @"hour" : [NSString stringWithFormat:@"%d",hour]
                               };
    [[AFNwebManager shareAFNWebManager]SOAPData:rainDict soapMethod:@"GetRainData" success:^(id responseObject) {
        rainDataBlock(responseObject);
    } failure:^{
        
    }];
}

#pragma mark -- 获取某个站点数据信息（气温、降水、风等）
- (void)getStationDataByStationNum:(NSString *)stationNum ElementType:(NSString *)element showNumber:(int)number StationBlock:(GETDATABLOCK)stationDataBlock{
    NSDictionary *dict = @{
                           @"resultType" : @"json",
                           @"stationNum": stationNum,
                           @"element":element,
                           @"count":[NSString stringWithFormat:@"%d",number]
                           };
    [[AFNwebManager shareAFNWebManager]SOAPData:dict soapMethod:@"GetStationLastData" success:^(id responseObject) {
        stationDataBlock(responseObject);
    } failure:^{
        
    }];
}

#pragma mark -- 获取下达维护任务的站点信息
- (void)getTaskStationWith:(NSArray *)paramsArray success:(void (^)(id responseObject))successData
{
    
    NSString *stationType,*city,*xian;
    if([paramsArray[1] isEqualToString:@"全部"])
    {
        stationType = @"";
    }
    else if ([paramsArray[1] isEqualToString:@"两要素站"])
    {
        stationType = @"T2";
    }
    else if ([paramsArray[1] isEqualToString:@"四要素站"])
    {
        stationType = @"T4";
    }
    else
    {
        stationType = @"T1";
    }
    
    if([paramsArray[2] isEqualToString:@"全部"])
    {
        city = @"";
    }
    else
    {
        city = paramsArray[2];
    }
    if([paramsArray[3] isEqualToString:@"全部"])
    {
        xian = @"";
    }
    else
    {
        xian = paramsArray[3];
    }
    
    NSDictionary *params = @{@"resultType":@"json",
                             @"station":paramsArray[0],
                             @"stationType":stationType,
                             @"city":city,
                             @"xian":xian
                             };
    
    [[AFNwebManager shareAFNWebManager] SOAPData:params soapMethod:@"GetStationInfoListForTask" success:^(id responseObject) {
        
        NSMutableArray *modelArray = [NSMutableArray array];
     
        for(NSDictionary *dict in responseObject)
        {
            
            StationInfoModel * model = [[StationInfoModel alloc]init];
            model.stationName = [dict objectForKey:@"站名"];
            model.stationType = [dict objectForKey:@"站点类型"];
            model.stationNum = [dict objectForKey:@"站号"];
            model.position = [dict objectForKey:@"位置"];
            model.navigationPosition  =[dict objectForKey:@"导航位置"];
            model.isSelected = NO;
            [modelArray addObject:model];
        }
//        if(modelArray.count>0)
//        {
            successData(modelArray);
//        }
        
    } failure:^{
        
    }];
    
}

#pragma mark -- 获取leiDaSum
- (void)getLeiDaSumDataWithBlock:(GETDATABLOCK)leiDaBlock{
    [[AFNwebManager shareAFNWebManager]SOAPData:nil soapMethod:@"LeiDaSum" success:^(id responseObject) {
        leiDaBlock(responseObject);
    } failure:^{
     
    }];
}

#pragma mark -- 获取leiDa
- (void)getLeiDaDataWithNum:(int)num Block:(GETDATABLOCK)numBlock{
    NSDictionary *dict = @{@"Num":[NSString stringWithFormat:@"%d",num]};
    [[AFNwebManager shareAFNWebManager]SOAPData:dict soapMethod:@"LeiDa" success:^(id responseObject) {
        HLog(@"%@",responseObject);
    } failure:^{
     
    }];
}

#pragma mark -- 用户登录检测
- (void)userLoginAndGetUserInfoByUserName:(NSString *)userName Password:(NSString *)password Block:(GETDATABLOCK)loginBlock{
    NSDictionary *dict = @{
                           @"userName":userName,
                           @"password":password,
                           @"resultType":@"json"
                           };
    [[AFNwebManager shareAFNWebManager]SOAPData:dict soapMethod:@"CheckUserInfo" success:^(id responseObject) {
        HLog(@"%@",responseObject);
        loginBlock(responseObject);
    } failure:^{

    }];
}

#pragma mark -- 下达运维任务
- (void)addMaintainTaskWithStationNum:(NSString *)stationNum userID:(NSString *)userID  taskType:(NSString *)type remarks:(NSString *)remark Block:(GETDATABLOCK)addTaskBlock{
    NSDictionary * dict = @{
                            @"stationNumList":stationNum,
                            @"userID":userID,
                            @"taskType":type,
                            @"remarks":remark
                            };
    
    [[AFNwebManager shareAFNWebManager] SOAPData:dict soapMethod:@"AddWorkTask" success:^(id responseObject) {
        HLog(@"%@",responseObject);
    } failure:^{
        NSLog(@"error");
    }];
}

#pragma mark -- 获取台站状态
- (void)getStationStatusWithBlock:(GETDATABLOCK)stationStatusBlock{
    NSDictionary *dict = @{@"resultType":@"json"};
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
    [[AFNwebManager shareAFNWebManager] SOAPData:dict soapMethod:@"GetStationStatus" success:^(id responseObject) {
        NSLog(@"%@",responseObject);
        
        NSArray *dataArray = responseObject;
        for (NSDictionary *dict in dataArray) {
            
            StatusModel *model = [[StatusModel alloc] init];
            model.stationNum = dict[@"站号"];
            model.status = dict[@"颜色码"];
            [array addObject:model];
        }
        
        stationStatusBlock(array);
    } failure:^{

    }];
}

@end
